package com.zensation.ticklemepanda;

import com.zensation.ticklemepanda.definition.GlobalDefine;
import com.zensation.ticklemepanda.util.TickleMePandaSharedPreference;
import com.zensation.ticklemepanda.util.SoundManager;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.text.style.LineHeightSpan.WithDensity;
import android.util.Log;

public class MyAnimal {
	// ========================Constant Define ====================
	// watch and blink
	private static int BMP_WATCH_BLINK_ROW = 3;
	private static int BMP_WATCH_BLINK_COLUMNS = 6;
	// cry
	private static int BMP_CRY_ROW = 5;
	private static int BMP_CRY_COLUMNS = 3;
	// giggle
	private static int BMP_GIGGLE_ROW = 1;
	private static int BMP_GIGGLE_COLUMNS = 8;
	// snooze
	private static int BMP_SNOOZE_ROW = 2;
	private static int BMP_SZOOZE_COLUMNS = 6;
	// asleep
	private static int BMP_ASLEEP_ROW = 2;
	private static int BMP_ASLEEP_COLUMNS = 5;
	// sleep
	private static int BMP_SLEEP_ROW = 2;
	private static int BMP_SLEEP_COLUMNS = 3;
	// wake up
	private static int BMP_WAKE_UP_ROW = 2;
	private static int BMP_WAKE_UP_COLUMNS = 5;
	// ========================Variable Define======================
	/**
	 * watch and blink
	 */
	private static Bitmap watch_blinkBitmap;
	private static Bitmap cryBitmap;
	private static Bitmap giggleBitmap;
	private static Bitmap snoozeBitmap;
	private static Bitmap asleepBitmap;
	private static Bitmap sleepBitmap;
	private static Bitmap wakeUpBitmap;

	//
	private Rect src, dst;
	private Paint paint;
	private int modeOfAnimal;
	private int widthBitmap, heightBitmap;
	private int widthScreen, heightScreen;
	private int currentFrameWidth = 0;
	private int currentFrameHeight = 0;

	private Context mContext;
	private boolean isCheckEndFrame = false;

	//
	private TickleMePandaSharedPreference funnyAnimalGameSharedPreference;

	public MyAnimal(Context mContext, int modeOfAnimal, int widthScreen, int heightScreen) {
		this.widthScreen = widthScreen;
		this.heightScreen = heightScreen;
		this.mContext = mContext;
		// initialize
		funnyAnimalGameSharedPreference = TickleMePandaSharedPreference.getInstance(mContext);
		/**
		 * initialize animation
		 */
		// if(this.heightScreen < 960){
		// watch and blink
		if (watch_blinkBitmap == null)
			watch_blinkBitmap = BitmapFactory.decodeResource(mContext.getResources(),
					R.drawable.watch_blink);
		// cry
		if (cryBitmap == null)
			cryBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.cry);
		// giggle
		if (giggleBitmap == null)
			giggleBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.giggle);
		// snooze
		if (snoozeBitmap == null)
			snoozeBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.snooze);
		// asleep
		if (asleepBitmap == null)
			asleepBitmap = BitmapFactory.decodeResource(mContext.getResources(),
					R.drawable.curling_down_sleep);
		// sleep
		if (sleepBitmap == null)
			sleepBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.sleep);
		// wake up
		if (wakeUpBitmap == null)
			wakeUpBitmap = BitmapFactory
					.decodeResource(mContext.getResources(), R.drawable.wake_up);

		// get size of sprite
		widthBitmap = watch_blinkBitmap.getWidth() / BMP_WATCH_BLINK_COLUMNS;
		heightBitmap = watch_blinkBitmap.getHeight() / BMP_WATCH_BLINK_ROW;
		// design for each screen configuration
		if (this.heightScreen < GlobalDefine.TAB_SCREEN_SIZE
				&& this.heightScreen > GlobalDefine.HDPI_SCREEN_SIZE)
			dst = new Rect(widthScreen / 2 - 4 * widthBitmap / 5, heightScreen / 2 - 4
					* heightBitmap / 5, widthScreen / 2 + 4 * widthBitmap / 5, heightScreen / 2 + 4
					* heightBitmap / 5);
		else if (this.heightScreen < GlobalDefine.HDPI_SCREEN_SIZE
				&& this.heightScreen > GlobalDefine.MDPI_SCREEN_SIZE)// for mdpi
																		// screen
			dst = new Rect(widthScreen / 2 - widthBitmap / 2, heightScreen / 2 - heightBitmap / 2,
					widthScreen / 2 + widthBitmap / 2, heightScreen / 2 + heightBitmap / 2);
		else if (this.heightScreen < GlobalDefine.MDPI_SCREEN_SIZE) // for ldpi
																	// screen
			dst = new Rect(widthScreen / 2 - 2 * widthBitmap / 3, heightScreen / 2 - 2
					* heightBitmap / 3, widthScreen / 2 + 2 * widthBitmap / 3, heightScreen / 2 + 2
					* heightBitmap / 3);
		else {
			// for tablet
			if (funnyAnimalGameSharedPreference.getDensityValue() > GlobalDefine.HDPI_DENSITY)
				dst = new Rect(widthScreen / 2 - widthBitmap, heightScreen / 2 - heightBitmap,
						widthScreen / 2 + widthBitmap, heightScreen / 2 + heightBitmap);
			else
				dst = new Rect(widthScreen / 2 - widthBitmap, heightScreen / 2 - heightBitmap,
						widthScreen / 2 + widthBitmap, heightScreen / 2 + heightBitmap);
		}
		if (modeOfAnimal != 0)
			this.modeOfAnimal = modeOfAnimal;
		else
			this.modeOfAnimal = 1;
		src = new Rect(0, 0, widthBitmap, heightBitmap);
		paint = new Paint();
		paint.setAntiAlias(true);

	}

	public void onDraw(Canvas canvas) {
		switch (modeOfAnimal) {
		case GlobalDefine.WATCH_BLINK:
			watchAndBlink(canvas);
			break;
		case GlobalDefine.GIGGLE:
			// play giggle sound
			SoundManager.getGiggleSound(mContext).play();
			SoundManager.getSleepSound(mContext).pause();
			SoundManager.getCrySound(mContext).pause();
			SoundManager.getWakeUpSound(mContext).pause();
			// draw giggle animation
			giggle(canvas);
			break;
		case GlobalDefine.CRY:
			// play cry sound
			SoundManager.getCrySound(mContext).play();
			SoundManager.getGiggleSound(mContext).pause();
			SoundManager.getSleepSound(mContext).pause();
			SoundManager.getSnoozeSound(mContext).pause();
			SoundManager.getWakeUpSound(mContext).pause();
			// draw cry animation
			cry(canvas);
			break;
		case GlobalDefine.SNOOZE:
			// play snooze sound
			SoundManager.getCrySound(mContext).pause();
			SoundManager.getGiggleSound(mContext).pause();
			SoundManager.getSleepSound(mContext).pause();
			SoundManager.getSnoozeSound(mContext).play();
			SoundManager.getWakeUpSound(mContext).pause();
			// draw snooze animation
			snooze(canvas);
			break;
		case GlobalDefine.ASLEEP:
			// // asleep
			// if (asleepBitmap == null)
			// asleepBitmap =
			// BitmapFactory.decodeResource(mContext.getResources(),
			// R.drawable.curling_down_sleep);
			// SoundManager.getSleepSound(mContext).play();
			// SoundManager.getGiggleSound(mContext).pause();
			// SoundManager.getCrySound(mContext).pause();
			// draw asleep animation
			asleep(canvas);
			break;

		case GlobalDefine.SLEEP:
			// sleep
			// if (sleepBitmap == null)
			// sleepBitmap =
			// BitmapFactory.decodeResource(mContext.getResources(),
			// R.drawable.sleep);
			// play sleep sound
			SoundManager.getCrySound(mContext).pause();
			SoundManager.getGiggleSound(mContext).pause();
			SoundManager.getSleepSound(mContext).play();
			SoundManager.getSnoozeSound(mContext).pause();
			SoundManager.getWakeUpSound(mContext).pause();
			// draw sleep animation
			sleep(canvas);
			break;
		case GlobalDefine.WAKE_UP:
			// wake up
			if (wakeUpBitmap == null)
				wakeUpBitmap = BitmapFactory.decodeResource(mContext.getResources(),
						R.drawable.wake_up);
			// play sound wake up
			SoundManager.getCrySound(mContext).pause();
			SoundManager.getGiggleSound(mContext).pause();
			SoundManager.getSleepSound(mContext).pause();
			SoundManager.getSnoozeSound(mContext).pause();
			SoundManager.getWakeUpSound(mContext).play();

			// draw wake up animation
			wakeUp(canvas);
			break;
		default:
			break;
		}
	}

	/**
	 * draw panda watch and blink
	 * 
	 * @param canvas
	 */
	private void watchAndBlink(Canvas canvas) {
		currentFrameWidth = ++currentFrameWidth % BMP_WATCH_BLINK_COLUMNS;
		if (currentFrameWidth == 0)
			currentFrameHeight = ++currentFrameHeight % BMP_WATCH_BLINK_ROW;

		int srcX = currentFrameWidth * widthBitmap;
		int srcY = currentFrameHeight * heightBitmap;
		if (currentFrameWidth == (BMP_WATCH_BLINK_COLUMNS - 1)
				&& currentFrameHeight == (BMP_WATCH_BLINK_ROW - 1)) {
			funnyAnimalGameSharedPreference.putScoreValue(funnyAnimalGameSharedPreference
					.getScoreValue() + 1);
		}
		Rect src = new Rect(srcX, srcY, srcX + widthBitmap, srcY + heightBitmap);
		canvas.drawBitmap(watch_blinkBitmap, src, dst, paint);
	}

	/**
	 * draw panda cry
	 * 
	 * @param canvas
	 */
	private void cry(Canvas canvas) {
		currentFrameWidth = ++currentFrameWidth % BMP_CRY_COLUMNS;
		if (currentFrameWidth == 0)
			currentFrameHeight = ++currentFrameHeight % BMP_CRY_ROW;
		if (currentFrameHeight == (BMP_CRY_ROW - 1) && currentFrameWidth == (BMP_CRY_COLUMNS - 1)) {
			funnyAnimalGameSharedPreference.putScoreValue(funnyAnimalGameSharedPreference
					.getScoreValue() - 3);
			if (funnyAnimalGameSharedPreference.getScoreValue() < 0)
				funnyAnimalGameSharedPreference.putScoreValue(0);
			currentFrameHeight = 3;
			currentFrameWidth = 0;
		}
		int srcX = currentFrameWidth * widthBitmap;
		int srcY = currentFrameHeight * heightBitmap;
		Rect src = new Rect(srcX, srcY, srcX + widthBitmap, srcY + heightBitmap);
		canvas.drawBitmap(cryBitmap, src, dst, paint);
	}

	/**
	 * draw panda giggle
	 * 
	 * @param canvas
	 */
	private void giggle(Canvas canvas) {
		currentFrameWidth++;
		if (currentFrameWidth == BMP_GIGGLE_COLUMNS - 1)
			funnyAnimalGameSharedPreference.putScoreValue(funnyAnimalGameSharedPreference
					.getScoreValue() + 3);

		if (currentFrameWidth > BMP_GIGGLE_COLUMNS - 1) {
			if (!funnyAnimalGameSharedPreference.getGiggleByTap())
				currentFrameWidth = 4;
			else {
				setModeOfAnimal(GlobalDefine.WATCH_BLINK);
				currentFrameWidth = 0;
			}
		}

		int srcX = currentFrameWidth * widthBitmap;
		int srcY = 0;
		Rect src = new Rect(srcX, srcY, srcX + widthBitmap, srcY + heightBitmap);
		canvas.drawBitmap(giggleBitmap, src, dst, paint);
	}

	/**
	 * draw panda snooze
	 * 
	 * @param canvas
	 */
	private void snooze(Canvas canvas) {
		currentFrameWidth = ++currentFrameWidth % BMP_SZOOZE_COLUMNS;
		if (currentFrameWidth == 0)
			currentFrameHeight = ++currentFrameHeight % BMP_SNOOZE_ROW;

		if (currentFrameWidth >= (BMP_SZOOZE_COLUMNS - 1)
				&& currentFrameHeight >= (BMP_SNOOZE_ROW - 1)) {
			setModeOfAnimal(GlobalDefine.ASLEEP);
			currentFrameHeight = 0;
			currentFrameWidth = 0;
		}

		int srcX = currentFrameWidth * widthBitmap;
		int srcY = currentFrameHeight * heightBitmap;
		Rect src = new Rect(srcX, srcY, srcX + widthBitmap, srcY + heightBitmap);
		canvas.drawBitmap(snoozeBitmap, src, dst, paint);
	}

	/**
	 * draw panda curling down sleep
	 * 
	 * @param canvas
	 */
	private void asleep(Canvas canvas) {
		currentFrameWidth = ++currentFrameWidth % BMP_ASLEEP_COLUMNS;
		if (currentFrameWidth == 0) {
			currentFrameHeight = ++currentFrameHeight % BMP_ASLEEP_ROW;
		}
		if (currentFrameWidth >= (BMP_ASLEEP_COLUMNS - 1)
				&& currentFrameHeight >= (BMP_ASLEEP_ROW - 1)) {
			setModeOfAnimal(GlobalDefine.SLEEP);
			// currentFrameHeight = 0;
			// currentFrameWidth = 0;
		}

		int srcX = currentFrameWidth * widthBitmap;
		int srcY = currentFrameHeight * heightBitmap;
		Rect src = new Rect(srcX, srcY, srcX + widthBitmap, srcY + heightBitmap);
		canvas.drawBitmap(asleepBitmap, src, dst, null);
	}

	/**
	 * draw panda sleep
	 * 
	 * @param canvas
	 */
	private void sleep(Canvas canvas) {
		currentFrameWidth = ++currentFrameWidth % BMP_SLEEP_COLUMNS;
		if (currentFrameWidth == (BMP_SLEEP_COLUMNS - 1))
			isCheckEndFrame = true;
		if (currentFrameWidth == 0 && isCheckEndFrame) {
			currentFrameHeight = ++currentFrameHeight % BMP_SLEEP_ROW;
			isCheckEndFrame = false;
		}
		if (currentFrameWidth == (BMP_SLEEP_COLUMNS - 1)
				&& currentFrameHeight == (BMP_SLEEP_ROW - 1)) {
			funnyAnimalGameSharedPreference.putScoreValue(funnyAnimalGameSharedPreference
					.getScoreValue() + GlobalDefine.SNOOZE_SCORE_BONUS);
			if (funnyAnimalGameSharedPreference.getScoreValue() < 0)
				funnyAnimalGameSharedPreference.putScoreValue(0);
		}
		int srcX = currentFrameWidth * widthBitmap;
		int srcY = 0;
		Rect src = new Rect(srcX, srcY, srcX + widthBitmap, srcY + heightBitmap);
		canvas.drawBitmap(sleepBitmap, src, dst, paint);
	}

	/**
	 * draw panda wake up
	 * 
	 * @param canvas
	 */
	private void wakeUp(Canvas canvas) {

		currentFrameWidth = ++currentFrameWidth % BMP_WAKE_UP_COLUMNS;
		if (currentFrameWidth == 0)
			currentFrameHeight = ++currentFrameHeight % BMP_WAKE_UP_ROW;
		if (currentFrameWidth >= (BMP_WAKE_UP_COLUMNS - 1)
				&& currentFrameHeight >= (BMP_WAKE_UP_ROW - 1)) {
			setModeOfAnimal(GlobalDefine.WATCH_BLINK);
		}
		int srcX = currentFrameWidth * widthBitmap;
		int srcY = currentFrameHeight * heightBitmap;
		Rect src = new Rect(srcX, srcY, srcX + widthBitmap, srcY + heightBitmap);
		canvas.drawBitmap(wakeUpBitmap, src, dst, paint);
	}

	/**
	 * set collision on touch event
	 * 
	 * @param point
	 * @return
	 */
	public boolean isCollision(Point point) {
		if (this.heightScreen < GlobalDefine.TAB_SCREEN_SIZE) {
			if (((widthScreen / 2 - widthBitmap / 2) < point.x)
					&& (point.x < widthScreen / 2 + widthBitmap / 2)
					&& (point.y < (heightScreen / 2 + heightBitmap / 2))
					&& ((heightScreen / 2 - heightBitmap / 6) < point.y))
				return true;
			else
				return false;
		} else {
			if (((widthScreen / 2 - widthBitmap / 2) < point.x)
					&& (point.x < widthScreen / 2 + widthBitmap / 2)
					&& (point.y < (heightScreen / 2 + 7 * heightBitmap / 10))
					&& ((heightScreen / 2 - heightBitmap / 6) < point.y))
				return true;
			else
				return false;
		}
	}

	/**
	 * set collision face of panda
	 * 
	 * @param point
	 * @return
	 */
	public boolean isCollisionFace(Point point) {
		if (((widthScreen / 2 - widthBitmap / 2) < point.x)
				&& (point.x < widthScreen / 2 + widthBitmap / 2) && (point.y < (heightScreen / 2))
				&& ((heightScreen / 2 - heightBitmap / 2) < point.y))
			return true;
		else
			return false;
	}

	/**
	 * set collision to the feet of panda
	 * 
	 * @param point
	 * @return
	 */
	public boolean isCollisionPandaFeet(Point point) {
		if (this.heightScreen < GlobalDefine.TAB_SCREEN_SIZE) {
			if (((widthScreen / 2 + widthBitmap / 2) < point.x)
					&& (point.x < widthScreen / 2 + 7 * widthBitmap / 5)
					&& (point.y < (heightScreen / 2 + 3 * heightBitmap / 5))
					&& ((heightScreen / 2 + heightBitmap / 5) < point.y))
				return true;
			else
				return false;
		} else {
			if (((widthScreen / 2 + widthBitmap / 2) < point.x)
					&& (point.x < widthScreen / 2 + 2 * widthBitmap)
					&& (point.y < (heightScreen / 2 + 4 * heightBitmap / 5))
					&& ((heightScreen / 2 + heightBitmap / 2) < point.y))
				return true;
			else
				return false;
		}

	}

	public void setModeOfAnimal(int modeOfAnimal) {
		this.modeOfAnimal = modeOfAnimal;
	}

	public int getModeOfAnimal() {
		return modeOfAnimal;
	}

	public void setCurrentFrameWidth(int currentFrameWidth) {
		this.currentFrameWidth = currentFrameWidth;
	}

	public int getCurrentFrameWidth() {
		return currentFrameWidth;
	}

	public void setCurrentFrameHeight(int currentFrameHeight) {
		this.currentFrameHeight = currentFrameHeight;
	}

	public int getCurrentFrameHeight() {
		return currentFrameHeight;
	}
}
